package com.dzt.androidframework.database.taskdetail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.constant.AppConstant;
import com.dzt.androidframework.database.addedittask.AddEditActivity;
import com.dzt.androidframework.database.addedittask.AddEditFragment;
import com.dzt.androidframework.fragment.FragmentBase;
import com.dzt.androidframework.utils.JLogKit;
import com.google.common.base.Preconditions;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Created by M02323 on 2017/6/12.
 */
@ContentView(R.layout.fragment_taskdetail)
public class TaskDetailFragment extends FragmentBase implements TaskDetailContract.View {
	private static final String ARGUMENT_TASK_ID = "TASK_ID";
	private TaskDetailContract.Presenter presenter;
	@ViewInject(R.id.task_detail_title)
	private TextView tvDetailTitle;
	@ViewInject(R.id.task_detail_description)
	private TextView tvDetailDescription;
	@ViewInject(R.id.task_detail_complete)
	private CheckBox cbDetailCompleteStatus;

	public static TaskDetailFragment newInstance(String taskId) {
		Bundle arguments = new Bundle();
		arguments.putString(ARGUMENT_TASK_ID, taskId);
		TaskDetailFragment fragment = new TaskDetailFragment();
		fragment.setArguments(arguments);
		return fragment;
	}

	@Override
	protected void initWidgets() {
		setHasOptionsMenu(true);
		// Set up floating action button
		FloatingActionButton fab =
				(FloatingActionButton) getActivity().findViewById(R.id.fab_edit_task);

		fab.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				presenter.editTask();
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		presenter.start();
	}

	@Override
	public void setPresenter(TaskDetailContract.Presenter presenter) {
		this.presenter = checkNotNull(presenter);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_delete:
				presenter.deleteTask();
				return true;
		}
		return false;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.taskdetail_fragment_menu, menu);
	}

	@Override
	public void setLoadingIndicator(boolean active) {
		if (active) {
			tvDetailTitle.setText("");
			tvDetailDescription.setText(getString(R.string.loading));
		}
	}

	@Override
	public void reLoadTask() {
		getActivity().setResult(Activity.RESULT_OK);
		getActivity().finish();
	}

	@Override
	public void showMissingTask() {
		tvDetailTitle.setText("");
		tvDetailDescription.setText(getString(R.string.no_data));
	}

	@Override
	public void hideTitle() {
		tvDetailTitle.setVisibility(View.GONE);
	}

	@Override
	public void showTitle(String title) {
		tvDetailTitle.setVisibility(View.VISIBLE);
		tvDetailTitle.setText(title);
	}

	@Override
	public void hideDescription() {
		tvDetailDescription.setVisibility(View.GONE);
	}

	@Override
	public void showDescription(String description) {
		tvDetailDescription.setVisibility(View.VISIBLE);
		tvDetailDescription.setText(description);
	}

	@Override
	public void showCompletionStatus(boolean complete) {
		Preconditions.checkNotNull(cbDetailCompleteStatus);
		JLogKit.getInstance().i("showCompletionStatus complete = " + complete);
		cbDetailCompleteStatus.setChecked(complete);
		cbDetailCompleteStatus.setOnCheckedChangeListener(
				new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						if (isChecked) {
							presenter.completeTask();
						} else {
							presenter.activateTask();
						}
					}
				});
	}

	@Override
	public void showEditTask(String taskId) {
		Intent intent = new Intent(getContext(), AddEditActivity.class);
		intent.putExtra(AddEditFragment.ARGUMENT_EDIT_TASK_ID, taskId);
		startActivityForResult(intent, AppConstant.REQUEST_EDIT_TASK);
	}

	@Override
	public void showTaskDeleted() {
		getActivity().finish();
	}

	@Override
	public void showTaskMarkedComplete() {
		showToast("任务标记为完成");
		getActivity().setResult(Activity.RESULT_OK);
	}

	@Override
	public void showTaskMarkedActive() {
		showToast("任务标记被激活");
		getActivity().setResult(Activity.RESULT_OK);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		presenter.result(requestCode, resultCode);
	}

	@Override
	public boolean isActive() {
		return isAdded();
	}
}
